package com.sf.fy.uav.applet.popscreen;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sf.fy.uav.common.enums.CommJumpTypeEnum;
import com.sf.fy.uav.common.enums.EjectFrequencyEnum;
import com.sf.fy.uav.common.enums.PopUpScreenStatusEnum;
import com.sf.fy.uav.common.request.operate.PopScreenReq;
import com.sf.fy.uav.common.request.operate.PopScreenRequest;
import com.sf.fy.uav.common.response.operational.PopUpScreenResponse;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.common.utils.RespJsonPageData;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.controller.PopUpScreenRest;
import com.sf.fy.uav.controller.RecordModuleDurationRest;
import com.sf.fy.uav.entity.PopUpScreen;
import com.sf.fy.uav.entity.RecordModuleDuration;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ta136817
 */
@RestController
@RequestMapping("/home/")
@RequiredArgsConstructor
@Tag(name = "弹屏管理", description = "弹屏管理")
public class PopScreenController {

    private final PopUpScreenRest popUpScreenRest;

    private final RecordModuleDurationRest recordModuleDurationRest;

    private final static String SPLIT_CHAR = "-1";
    private final static String SPLIT_CHAR_TWO = "-2";

    @GetMapping("get-pop-screen")
    @Operation(tags = "获取弹屏信息列表", description = "获取弹屏信息列表")
    @CrossOrigin
    @Parameter(name = "page", description = "页码")
    @Parameter(name = "size", description = "单页条数")
    @Parameter(name = "title", description = "弹屏标题")
    @Parameter(name = "status", description = "弹屏状态")
    public RespJsonPageData<PopUpScreenResponse> getCoupons(@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) String title
            , @RequestParam(required = false) Integer status) {
        PopScreenRequest popScreenRequest = new PopScreenRequest();
        popScreenRequest.setPage(page);
        popScreenRequest.setSize(size);
        popScreenRequest.setTitle(title);
        popScreenRequest.setStatus(status);
        Page<PopUpScreen> popUpScreenPageList = popUpScreenRest.getPopUpScreenPageList(popScreenRequest);
        List<PopUpScreen> records = popUpScreenPageList.getRecords();
        List<String> ids = records.stream().map(PopUpScreen::getId).toList();
        List<RecordModuleDuration> recordModuleDurations;
        if (!CollectionUtils.isEmpty(ids)) {
            recordModuleDurations = recordModuleDurationRest.selectRecordModuleDurationListBySubjectIds(ids);
        } else {
            recordModuleDurations = new ArrayList<>();
        }
        List<PopUpScreenResponse> popUpScreenResponses = BeanUtil.copyProperties(popUpScreenPageList.getRecords(), PopUpScreenResponse.class);
        popUpScreenResponses.forEach(popUpScreenResponse -> {
            popUpScreenResponse.setStatusName(PopUpScreenStatusEnum.getName(popUpScreenResponse.getStatus()));
            popUpScreenResponse.setEjectFrequencyName(EjectFrequencyEnum.getName(popUpScreenResponse.getEjectFrequency()));
            popUpScreenResponse.setJumpTypeName(CommJumpTypeEnum.getDesc(popUpScreenResponse.getJumpType()));
            if (EjectFrequencyEnum.EVERY_FEW_DAYS.getCode().equals(popUpScreenResponse.getEjectFrequency()) && ObjectUtils.isNotEmpty(popUpScreenResponse.getDays())) {
                popUpScreenResponse.setEjectFrequencyName("每隔" + popUpScreenResponse.getDays() + "天弹出");
            }
            AtomicInteger ejectTimesSum = new AtomicInteger();
            AtomicInteger clickTimesSum = new AtomicInteger();
            if (!CollectionUtils.isEmpty(recordModuleDurations)) {
                recordModuleDurations.forEach(recordModuleDuration -> {
                    if (recordModuleDuration.getSubjectId().equals(popUpScreenResponse.getId())) {
                        if (StringUtils.isNotBlank(recordModuleDuration.getBuriedPointNumber()) && recordModuleDuration.getBuriedPointNumber().contains(SPLIT_CHAR)) {
                            ejectTimesSum.incrementAndGet();
                        }
                        if (StringUtils.isNotBlank(recordModuleDuration.getBuriedPointNumber()) && recordModuleDuration.getBuriedPointNumber().contains(SPLIT_CHAR_TWO)) {
                            clickTimesSum.incrementAndGet();
                        }
                    }
                });
            }
            popUpScreenResponse.setEjectTimes(ejectTimesSum.get());
            popUpScreenResponse.setClickTimes(clickTimesSum.get());
        });
        return RespJsonPageData.success(popUpScreenResponses, popUpScreenPageList.getTotal());
    }

    @PostMapping("add-pop-screen")
    public Rs<Integer> addPopScreen(@RequestBody PopScreenReq popScreenReq) {
        return popUpScreenRest.addPopScreen(popScreenReq);
    }

    @PostMapping("edit-pop-screen")
    public Rs<Integer> editPopScreen(@RequestBody PopScreenReq popScreenReq) {
        return popUpScreenRest.editPopScreen(popScreenReq);
    }

    @PostMapping("del-pop-screen")
    public Rs<Integer> delPopScreen(@RequestBody PopScreenReq popScreenReq) {
        return popUpScreenRest.delPopScreen(popScreenReq);
    }
}
